﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutomatedTestDataManagement.Models;

namespace AutomatedTestDataManagement.Controllers
{
    public class TestTestStepController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /TestTestStep/

        public ActionResult Index()
        {
            var testteststeps = db.TestTestSteps.Include(t => t.Test).Include(t => t.TestStep);
            return View(testteststeps.ToList());
        }

        //
        // GET: /TestTestStep/Details/5

        public ActionResult Details(int id = 0)
        {
            TestTestStep testteststep = db.TestTestSteps.Find(id);
            if (testteststep == null)
            {
                return HttpNotFound();
            }
            return View(testteststep);
        }

        //
        // GET: /TestTestStep/Create

        public ActionResult Create(int id = 0, int scenarioID = 0, int suiteID = 0)
        {
            if (id == 0)
            {
                ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName");
                ViewBag.StepNumber = 0;
            }
            else
            {
                ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName", id);
                int maxNum = 0;
                if (db.TestTestSteps.Where(ts => ts.TestID == id).Count() != 0)
                {
                    maxNum = db.TestTestSteps.Where(ts => ts.TestID == id).Max(ts => ts.StepNumber);
                }
                ViewBag.SelectedTestID = id;
                ViewBag.StepNumber = maxNum + 1;
            }

            if (scenarioID != 0)
            {
                ViewBag.ScenarioID = scenarioID;
            }

            if (suiteID != 0)
            {
                ViewBag.SuiteID = suiteID;
            }
            ViewBag.TestStepID = new SelectList(db.TestSteps, "TestStepID", "StepDescription");
            return View();
        }

        //
        // POST: /TestTestStep/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(TestTestStep testteststep)
        {
            if (ModelState.IsValid)
            {
                db.TestTestSteps.Add(testteststep);
                db.SaveChanges();

                // parent checking
                if (Request.Form["ScenarioID"] != null)
                {
                    if (Request.Form["SuiteID"] != null)
                    {
                        return RedirectToAction("Edit", "Test", new { id = testteststep.TestID, scenarioID = Request.Form["ScenarioID"], suiteID = Request.Form["SuiteID"] });
                    }
                    else
                    {
                        return RedirectToAction("Edit", "Test", new { id = testteststep.TestID, scenarioID = Request.Form["ScenarioID"] });
                    }
                }
                else
                {
                    return RedirectToAction("Edit", "Test", new { id = testteststep.TestID });
                }
            }

            if (Request.Form["ScenarioID"] != null) { ViewBag.ScenarioID = Request.Form["ScenarioID"]; }
            if (Request.Form["SuiteID"] != null) { ViewBag.SuiteID = Request.Form["SuiteID"]; }
            if (Request.Form["SelectedTestID"] != null) { ViewBag.SelectedTestID = Request.Form["SelectedTestID"]; }
            ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName", testteststep.TestID);
            ViewBag.TestStepID = new SelectList(db.TestSteps, "TestStepID", "StepDescription", testteststep.TestStepID);
            return View(testteststep);
        }

        //
        // GET: /TestTestStep/Edit/5

        public ActionResult Edit(int id = 0, int scenarioID = 0, int suiteID = 0)
        {
            TestTestStep testteststep = db.TestTestSteps.Find(id);
            if (testteststep == null)
            {
                return HttpNotFound();
            }

            if (scenarioID != 0)
            {
                ViewBag.ScenarioID = scenarioID;
            }

            if (suiteID != 0)
            {
                ViewBag.SuiteID = suiteID;
            }
            ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName", testteststep.TestID);
            ViewBag.TestStepID = new SelectList(db.TestSteps, "TestStepID", "StepDescription", testteststep.TestStepID);
            return View(testteststep);
        }

        //
        // POST: /TestTestStep/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(TestTestStep testteststep)
        {
            if (ModelState.IsValid)
            {
                db.Entry(testteststep).State = EntityState.Modified;
                db.SaveChanges();
                if (Request.Form["ScenarioID"] != null)
                {
                    if (Request.Form["SuiteID"] != null)
                    {
                        return RedirectToAction("Edit", "Test", new { id = testteststep.TestID, scenarioID = Request.Form["ScenarioID"], suiteID = Request.Form["SuiteID"] });
                    }
                    else
                    {
                        return RedirectToAction("Edit", "Test", new { id = testteststep.TestID, scenarioID = Request.Form["ScenarioID"] });
                    }
                }
                else
                {
                    return RedirectToAction("Edit", "Test", new { id = testteststep.TestID });
                }
            }
            ViewBag.TestID = new SelectList(db.Tests, "TestID", "TestName", testteststep.TestID);
            ViewBag.TestStepID = new SelectList(db.TestSteps, "TestStepID", "StepDescription", testteststep.TestStepID);
            return View(testteststep);
        }

        //
        // GET: /TestTestStep/Delete/5

        public ActionResult Delete(int id = 0, int testID = 0, int scenarioID = 0, int suiteID = 0, int testStepID = 0, string returnToView = "Edit")
        {
            TestTestStep testteststep = db.TestTestSteps.Find(id);
            if (testteststep == null)
            {
                return HttpNotFound();
            }
            if (testID != 0) { ViewBag.TestID = testID; }
            if (scenarioID != 0) { ViewBag.ScenarioID = scenarioID; }
            if (suiteID != 0) { ViewBag.SuiteID = suiteID; }
            if (testStepID != 0) { ViewBag.TestStepID = testStepID; }
            ViewBag.ReturnToView = returnToView;
            return View(testteststep);
        }

        //
        // POST: /TestTestStep/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var testID = Request.Form["TestID"];
            var scenarioID = Request.Form["ScenarioID"];
            var suiteID = Request.Form["SuiteID"];
            var returnToView = Request.Form["ReturnToView"];
            var testStepID = Request.Form["TestStepID"];

            TestTestStep testteststep = db.TestTestSteps.Find(id);
            db.TestTestSteps.Remove(testteststep);
            db.SaveChanges();

            if (testID != null)
            {
                if (scenarioID != null)
                {
                    if (suiteID != null)
                    {
                        return RedirectToAction(returnToView, "Test", new { id = testID, scenarioID = scenarioID, suiteID = suiteID });
                    }
                    else
                    {
                        return RedirectToAction(returnToView, "Test", new { id = testID, scenarioID = scenarioID });
                    }
                }
                else
                {
                    return RedirectToAction(returnToView, "Test", new { id = testID });
                }
            }
            else
            {
                if (testStepID != null)
                {
                    return RedirectToAction("Delete", "TestStep", new { id = testStepID });
                }
                else
                {
                    return RedirectToAction("Index", "Test");
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}